package com.syni.mdd.yhd.common.all.service.impl.market;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.yhd.common.all.constants.ResultMsg;
import com.syni.mdd.yhd.common.all.dao.jpa.market.BmsMarketCouponDao;
import com.syni.mdd.yhd.common.all.entity.market.BmsMarketCoupon;
import com.syni.mdd.yhd.common.all.service.impl.BaseService;
import com.syni.mdd.yhd.common.all.service.market.BmsMarketCouponService;
import com.syni.mdd.sjb.common.component.utils.common.utils.ParamsMap;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.yhd.common.all.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @className
 * @description TOO
 * @Author hwq
 * @DATE 2019/9/23 10:34
 * @VERSION 1.0
 **/
@Service
public class BmsMarketCouponServiceImpl extends BaseService<BmsMarketCoupon, Integer> implements BmsMarketCouponService {

    @Autowired
    BmsMarketCouponDao bmsMarketCouponDao;


    @Override
    public BmsMarketCouponDao getBaseDao() {
        return bmsMarketCouponDao;
    }

    @Override
    public Map<String, Object> produceCoupon(ParamsMap paramsMap) {
        String content = paramsMap.getString("content");
        List<Map> contentObj = JSON.parseArray(content,Map.class);
        if(contentObj.size() > 0){
            List<BmsMarketCoupon> bmsMarketCoupons = new ArrayList<>();
            for(Map<String,Object> con : contentObj){
                String prizeId = con.get("prizeId").toString();
                BmsMarketCoupon bmsMarketCoupon = null;
                if(StringUtils.isNotBlank(prizeId)){
                    bmsMarketCoupon = bmsMarketCouponDao.findByPrizeId(prizeId);
                }
                if(bmsMarketCoupon == null){
                    bmsMarketCoupon = new BmsMarketCoupon();
                    bmsMarketCoupon.setPrizeId(prizeId);
                    bmsMarketCoupon.setNewTime(new Date());
                }
                bmsMarketCoupons.add(bmsMarketCoupon);
            }
            if(bmsMarketCoupons.size() > 0) {
                bmsMarketCouponDao.saveAll(bmsMarketCoupons);
            }
            return ResultMapHelper.success(bmsMarketCoupons, ResultMsg.INSERT_SUCCESS);
        }
        return ResultMapHelper.success(new ArrayList<>(), ResultMsg.INSERT_SUCCESS);
    }
}
